<?php


namespace app\common\service;
use app\common\service\Base;
use think\Db;

class SkuPackageV2 extends Base
{

    /**
     * 获取尾货差异数
     * @date 2022年8月12日
     * author huanghuixiang
     * @param string $order_id
     * @param string $sku
     * @param string $size_name
     * @return int
     */
    public function getInfoByOrderSizeName($order_id,$sku, $size_name) {
        $result=db('sku_package_detail')->field('tail_goods_dff')->where(['order_id'=>$order_id,'sku'=>$sku,'size_name'=>$size_name])->group("size_name")->find();
        return $result['tail_goods_dff'];
    }

    /**
     * 根据sku、尺码-获取正品库存总数
     * @date 2022年8月27日
     * author huanghuixiang
     * @param string $sku
     * @param string $size_name
     * @return int
     */
    public function getSkuStockTotal($sku, $size_name) {
        $result=db('sku_stock')->field('sum(available_count) as total_num_one')->where(['sku'=>$sku,'size_name'=>$size_name])->group("size_name")->find();
        if (empty($result)) return 0;
        return $result['total_num_one'];
    }
    /**
     * 获取占用库存
     * @date 2022年8月27日
     * author huanghuixiang
     * @param string $order_id
     * @param string $sku
     * @param string $size_name
     * @return int
     */
    public function getSkuStockOccupy($order_id,$sku, $size_name) {
        $result=db('sku_package_detail')->field('sum(abs(tail_goods_dff)) as total_num')->where(['status'=>3,'sku'=>$sku,'size_name'=>$size_name])->find();
        if (empty($result)) return 0;
        return $result['total_num'];
    }
    /**
     * 统计出库明细的总数
     * @date 2022年8月29日
     * author huanghuixiang
     * @return int
     */
    public function getOutStackTotalNum($where=[]) {
        $where[]=['status','=',3];
        $list=db('sku_package_detail')->field('client_ordernum')->where($where)->group('client_ordernum')->select();
        foreach($list as $key=>$val){
            $total_num_need=0;//总库存数
            $total_num=0;//总件数
            $order_list= db('sku_package_detail')->field('sku,size_name,client_ordernum,tail_goods_dff')->where('client_ordernum',$val['client_ordernum'])->select();
            //获取订单下面的sku，size 总库存
            foreach ($order_list as $key_two=>$value){
                $countOut=$this->getSkuStockTotal($value['sku'],$value['size_name']);
                $total_num +=$value['tail_goods_dff'];
                $total_num_need +=$countOut;
            }
            //尾货差异总数为零的不显示
            if( $total_num==0){
                unset($list[$key]);
                continue;
            }
        }
        if (empty($list)) return 0;
        return count($list);
    }
    /**
     * 尾货管理-判断该订单、尺码是否存在入库单中
     * @date 2022年8月29日
     * @param string $ordernum
     * @param string $size_name
     * author huanghuixiang
     * @return int
     */
    public function doesItExitDeliveryOrder($ordernum,$size_name){
        $sql="SELECT t10.client_ordernum,t10.size_name from 
                (
                SELECT * FROM ( SELECT `t6`.*,`skst`.`available_count` FROM wx_sku_stock skst 
                RIGHT JOIN (
                 SELECT o.sku,o.id as order_id,o.tail_over_time,t2.is_tail_manage,t2.factory_id,t2.size_name,t2.client_ordernum,t2.order_count,t2.shicai_count, t2.jiaohuo_count,t2.daohuo_count,t2.jiaohuo_tail_count,(cast(t2.jiaohuo_count as signed) - cast(t2.daohuo_count as signed)) as tail_goods_dff, t2.tail_stock_in_out,sk.stock_finished,sk.ori_pic,f.name as factory_name FROM wx_order o 
        
                LEFT JOIN `wx_order_size` `t2` ON `o`.`id`=`t2`.`order_id` 
                LEFT JOIN `wx_sku` `sk` ON `o`.`sku`=`sk`.`sku` 
                LEFT JOIN `wx_factory` `f` ON `o`.`factory_id`=`f`.`id` WHERE  `o`.`status` IN (0,1) AND `t2`.`is_tail_manage` = 0 ) `t6` ON `t6`.`client_ordernum`=skst.client_ordernum and t6.size_name=skst.size_name 
                WHERE ( ( t6.tail_goods_dff >0 ) or ( (t6.tail_goods_dff < 0 and skst.available_count <> 0) ) ) AND ( t6.daohuo_count >0 ) ) aa 
                
                UNION ALL ( ( SELECT o.sku,o.id as order_id,o.tail_over_time,t2.is_tail_manage,t2.factory_id,t2.size_name,t2.client_ordernum,t2.order_count,t2.shicai_count, t2.jiaohuo_count,t2.daohuo_count,t2.jiaohuo_tail_count,(cast(t2.jiaohuo_count as signed) - cast(t2.daohuo_count as signed)) as tail_goods_dff, t2.tail_stock_in_out,sk.stock_finished,sk.ori_pic,f.name as factory_name,skst.available_count 
                
                FROM wx_order o 
                LEFT JOIN `wx_order_size` `t2` ON `o`.`id`=`t2`.`order_id` 
                LEFT JOIN `wx_sku` `sk` ON `o`.`sku`=`sk`.`sku` 
                LEFT JOIN `wx_factory` `f` ON `o`.`factory_id`=`f`.`id` LEFT JOIN `wx_sku_stock` `skst` ON `t2`.`client_ordernum`=skst.client_ordernum and t2.size_name=skst.size_name 
                
                WHERE  `t2`.`is_tail_manage` = -1 ) )
                ) 
                    t10  where  t10.client_ordernum='".$ordernum."' and t10.size_name='".$size_name."'  limit 1";
        $resault= Db::query($sql);
        return count($resault);
    }

}